package com.longdaji.decoration.ui.splash;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.longdaji.decoration.DecorationApplication;
import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.ui.guide.GuideActivity;
import com.longdaji.decoration.ui.login.LoginActivity;
import com.longdaji.decoration.ui.main.MainActivity;
import com.longdaji.decoration.utils.ImageUtil;
import com.longdaji.decoration.utils.PreferencesUtil;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class SplashActivity extends BaseActivity implements SplashContract.View {

    @BindView(R.id.iv_splash_image)
    ImageView mIvSplashImage;
    @BindView(R.id.tv_jump)
    TextView mIvJump;
    @Inject
    SplashContract.Presenter mPresenter;
    private Disposable mDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        if (mPresenter != null)
            mPresenter.setView(this);
        initView();
    }

    private void initView() {
        if (PreferencesUtil.getFirstLogin() == true) {
            PreferencesUtil.saveFirstLogin(false);
            startActivity(new Intent(SplashActivity.this, GuideActivity.class));
            finish();
        } else {
            mPresenter.getAd();
        }
    }

    @OnClick({R.id.tv_jump})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_jump:
                String userName = PreferencesUtil.getUserAccount();
                String passWord = PreferencesUtil.getUserPassword();
                if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(passWord)) {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    finish();
                } else {
                    mPresenter.autoLogin(userName, passWord);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void showAd(String url) {
        mIvJump.setVisibility(View.VISIBLE);
        ImageUtil.loadImage(this, url, mIvSplashImage);
        Observable.timer(3000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                        mDisposable = disposable;
                    }

                    @Override
                    public void onNext(@NonNull Long number) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //取消订阅
                        if (mDisposable != null && !mDisposable.isDisposed()) {
                            mDisposable.dispose();
                        }
                    }

                    @Override
                    public void onComplete() {
                        //取消订阅
                        if (mDisposable != null && !mDisposable.isDisposed()) {
                            mDisposable.dispose();
                        }
                        String userName = PreferencesUtil.getUserAccount();
                        String passWord = PreferencesUtil.getUserPassword();
                        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(passWord)) {
                            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                            finish();
                        } else {
                            mPresenter.autoLogin(userName, passWord);
                        }

                    }
                });


    }

    @Override
    protected void onDestroy() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
        super.onDestroy();
    }

    @Override
    public void showNoAd() {
        Observable.timer(3000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                        mDisposable = disposable;
                    }

                    @Override
                    public void onNext(@NonNull Long number) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //取消订阅
                        if (mDisposable != null && !mDisposable.isDisposed()) {
                            mDisposable.dispose();
                        }
                    }

                    @Override
                    public void onComplete() {
                        //取消订阅
                        if (mDisposable != null && !mDisposable.isDisposed()) {
                            mDisposable.dispose();
                        }
                        String userName = PreferencesUtil.getUserAccount();
                        String passWord = PreferencesUtil.getUserPassword();
                        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(passWord)) {
                            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                            finish();
                        } else {
                            mPresenter.autoLogin(userName, passWord);
                        }

                    }
                });

    }

    @Override
    public void goMainByUsername(Account account) {
        Account.setInstance(account);
        DecorationApplication.setJustLook(false);
        MainActivity.start(this);
        finish();
    }

    @Override
    public void showNimLoginFail() {
        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
        finish();
    }
}
